package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.LatentClient;
import cn.wolfcode.crm.query.LatentClientQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ILatentClientService;
import cn.wolfcode.crm.util.Dispose;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @author konki
 * @time   2018年6月19日 下午7:03:47
 * @email  konki@qq.com
 * @description 
 */
@Controller
@RequestMapping("latentClient")
public class LatentClientController {

	@Autowired
	private ILatentClientService latentClientService;

	@RequestMapping("outView")
	@RequiresPermissions(value = {"latentClient:outView", "正式客户界面"}, logical = Logical.OR)
	public String stateView() {
		return "latentClient/list";
	}


	@RequestMapping("selectStateByStatus")
	@ResponseBody
	@RequiresPermissions(value = {"latentClient:selectStateByStatus", "正式客户查询"}, logical = Logical.OR)
	public PageResult selectStateByStatus(LatentClientQueryObject qo) {
		return latentClientService.selectStateByStatus(qo);
	}

	@RequestMapping("saveOrUpdate")
	@ResponseBody
	@RequiresPermissions(value = {"latentClient:view", "正式客户添加和更新"}, logical = Logical.OR)
	public Dispose saveOrUpdate(LatentClient entity) {
		Dispose result = new Dispose();
		try {
			if (entity.getId() == null) {
				latentClientService.insert(entity);
			}else {
				latentClientService.updateByPrimaryKey(entity);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return result.mark("保存失败");
		}
	}

	/**
	 * 下拉框,查询
	 * @param
	 * @return
	 */
	@RequestMapping("changStatus")
	@ResponseBody
	//@RequiresPermissions(value = {"latentClient:changStatus", "把客户转如资源池"}, logical = Logical.OR)
	@RequiresPermissions("latentClient:changStatus")
	public Dispose changStatus(Long id) {
		Dispose result = new Dispose();
		try {
			latentClientService.changStatus(id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return result.mark("操作失败");
		}
	}

	@RequestMapping("plan")
	@ResponseBody
	//@RequiresPermissions(value = {"latentClient:plan", "把客户列入开发计划"}, logical = Logical.OR)
	@RequiresPermissions("latentClient:changStatus")
	public Dispose plan(Long id) {
		Dispose result = new Dispose();
		try {
			latentClientService.plan(id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return result.mark("操作失败");
		}
	}

	/**
	 * CLIENT_LATENT  资源池客户移入潜在客户
	 * @param id
	 * @return
	 */
	@RequestMapping("changeState2Latent")
	@ResponseBody
	public Dispose changeState2Latent(Long id) {

		Dispose result = new Dispose();
		try {
			latentClientService.changeState2Latent(id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return result.mark("吸纳失败");
		}

	}
	@RequestMapping("changeState")
	@ResponseBody
	public Dispose changeState(Long id) {

		Dispose result = new Dispose();
		try {
			latentClientService.changeState(id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return result.mark("更改状态失败");
		}

	}
	/**  根据客户id查询客户所有信息  */
	@RequestMapping("selectApplicantById")
	@ResponseBody
	public LatentClient selectApplicantById(Long id) {
		return latentClientService.selectApplicantById(id);
	}

	@RequestMapping("listAll")
	@ResponseBody
	public List<LatentClient> listAll() {
		return latentClientService.listAll();
	}


	@RequestMapping("queryClientById")
	@ResponseBody
	public LatentClient queryClientById(Long id){
		return latentClientService.selectByPrimaryKey(id);
	}
}
